home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / FFT and Complex Numbers Library / FFTLibrary.sit / FFT library ƒ / Complex ƒ / ComplexNumbers.h < prev    next >
Text File  |  1995-01-15  |  5KB  |  180 lines

  1. //
  2. //    File: ComplexNumbers.h
  3. //    Copyright ⌐1994-1995 James Wilson
  4. //    All rights reserved.
  5. //    
  6. //    This file is a more Macintosh-compatible version of the complex class
  7. //    used in the iostreams library. Among the changes, the size of the real and
  8. //    imaginary components are now doubles instead of long doubles. This is for PowerPC
  9. //    compatibility. I have replaced all references to int with long. The
  10. //    << and >> I/O operators are not in this class; the Macintosh interface has other
  11. //    and better ways to handle standard I/O with the user. I have also done my best
  12. //    to eliminate the use of friends and other questionable C++ constructs from this
  13. //    implementation. To match with with Macintosh class naming style conventions,
  14. //    the name of this class is Complex. Much of the implementation code is from
  15. //    the original complex source files, some modified to be more Macintosh and
  16. //    ANSI-C++ compatible.
  17.  
  18. #if defined(__SC__)
  19. #pragma once
  20. #endif
  21.  
  22. #ifndef __COMPLEXNUMBERS__
  23. #define __COMPLEXNUMBERS__
  24.  
  25. #if macintosh
  26.     #if defined(powerc)
  27.         #include <math.h>
  28.     #else
  29.         #if defined(__SC__)
  30.             #if __option(mc68881)
  31.                 #include <math.h>
  32.             #else
  33.                 #ifndef __SANE__
  34.                     #include <SANE.h>
  35.                 #endif
  36.             #endif
  37.         #else
  38.             #include <math.h>
  39.         #endif
  40.     #endif
  41. #else
  42.     #include <math.h>
  43. #endif
  44.  
  45. #if macintosh
  46.     #ifndef __TYPES__
  47.         enum {false, true};
  48.     #endif
  49. #else
  50.     enum {false, true};
  51. #endif
  52.  
  53. typedef double FP_TYPE;
  54.  
  55. class Complex {
  56. public:
  57.     
  58.     // Constructors
  59.     Complex();
  60.     Complex(const FP_TYPE re, const FP_TYPE im);
  61.     Complex(const Complex& copy);                // Complex's copy constructor
  62.     
  63.     // Indirect access functions
  64.     FP_TYPE& Real();
  65.     FP_TYPE& Imag();
  66.     
  67.     // These functions return only values not references.
  68.     FP_TYPE Re() const;
  69.     FP_TYPE Im() const;
  70.     
  71.     // Overloaded operators
  72.     
  73.     // operator+
  74.     Complex operator+(const Complex&) const;
  75.     Complex operator+(const FP_TYPE) const;
  76.     friend Complex operator+(const FP_TYPE, const Complex&);
  77.     
  78.     // operator-
  79.     Complex operator-(const Complex&) const;
  80.     Complex operator-(const FP_TYPE) const;
  81.     friend Complex operator-(const FP_TYPE, const Complex&);
  82.     
  83.     // operator*
  84.     Complex operator*(const Complex&) const;
  85.     Complex operator*(const FP_TYPE) const;
  86.     friend Complex operator*(const FP_TYPE, const Complex&);
  87.     
  88.     // operator/
  89.     Complex operator/(const Complex&) const;
  90.     Complex operator/(const FP_TYPE) const;
  91.     friend Complex operator/(const FP_TYPE, const Complex&);
  92.     
  93.     // operator&&
  94.     long operator&&(const Complex&) const;
  95.     long operator&&(const FP_TYPE) const;
  96.     friend long operator&&(const FP_TYPE, const Complex&);
  97.     
  98.     // operator||
  99.     long operator||(const Complex&) const;
  100.     long operator||(const FP_TYPE) const;
  101.     friend long operator||(const FP_TYPE, const Complex&);
  102.     
  103.     // operator==
  104.     long operator==(const Complex&) const;
  105.     long operator==(const FP_TYPE) const;
  106.     friend long operator==(const FP_TYPE, const Complex&);
  107.     
  108.     // operator !=
  109.     long operator!=(const Complex&) const;
  110.     long operator!=(const FP_TYPE) const;
  111.     friend long operator!=(const FP_TYPE, const Complex&);
  112.     
  113.     // Unary operators
  114.     long operator!() const;
  115.     
  116.     Complex operator-() const;
  117.     
  118.     // Overloaded operators returning references
  119.     
  120.     // operator=
  121.     Complex& operator=(const Complex&);
  122.     Complex& operator=(const FP_TYPE);
  123.     
  124.     // operator+=
  125.     Complex& operator+=(const Complex&);
  126.     Complex& operator+=(const FP_TYPE);
  127.     
  128.     // operator-=
  129.     Complex& operator-=(const Complex&);
  130.     Complex& operator-=(const FP_TYPE);
  131.     
  132.     // operator*=
  133.     Complex& operator*=(const Complex&);
  134.     Complex& operator*=(const FP_TYPE);
  135.     
  136.     // operator/=
  137.     Complex& operator/=(const Complex&);
  138.     Complex& operator/=(const FP_TYPE);
  139.     
  140. private:
  141.     FP_TYPE                real;
  142.     FP_TYPE                imag;
  143. };
  144.  
  145. // Prototypes of complex number functions
  146.  
  147. FP_TYPE Abs(const Complex&);
  148. FP_TYPE Mod(const Complex&);
  149. FP_TYPE Arg(const Complex&);
  150. Complex Conj(const Complex&);
  151.  
  152. // Inline member functions
  153.  
  154. inline Complex::Complex(void) : real(0.0), imag(0.0) { }
  155. inline Complex::Complex(const FP_TYPE re, const FP_TYPE im) : real(re), imag(im) { }
  156. inline Complex::Complex(const Complex& z) : real(z.real), imag(z.imag) { }
  157.  
  158. inline FP_TYPE& Complex::Real(void) { return real; }
  159. inline FP_TYPE& Complex::Imag(void) { return imag; }
  160.  
  161. inline FP_TYPE Complex::Re(void) const { return real; }
  162. inline FP_TYPE Complex::Im(void) const { return imag; }
  163.  
  164. inline long Complex::operator!(void) const
  165. {
  166.     return ((Re()==0) && (Im()==0)) ? true : false;
  167. }
  168.  
  169. inline Complex Complex::operator-(void) const
  170. {
  171.     return Complex(-Re(), -Im());
  172. }
  173.  
  174. inline FP_TYPE Mod(const Complex& z)
  175. {
  176.     return Abs(z);
  177. }
  178.  
  179. #endif
  180.